//
//  NSString+Verification.m
//  RMEducation
//
//  Created by tongbinLi on 16/3/14.
//  Copyright © 2016年 tongbinLi. All rights reserved.
//

#import "NSString+Verification.h"

@implementation NSString (Verification)

/*手机号码验证 MODIFIED BY HELENSONG*/
- (BOOL)isMobileNumber
{
    
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,183，170
     */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|7[0]|8[2378])\\d)\\d{7}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,152,155,156,185,186
     */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,1349,153,180,189
     */
    NSString * CT = @"^1((33|77|53|81|8[09])[0-9]|349)\\d{7}$";
    /**
     * 大陆地区固话及小灵通
     * 区号：010,020,021,022,023,024,025,027,028,029
     * 号码：七位或八位
     */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:self] == YES)
        || ([regextestcm evaluateWithObject:self] == YES)
        || ([regextestct evaluateWithObject:self] == YES)
        || ([regextestcu evaluateWithObject:self] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

//密码
- (BOOL)validatePassword
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,25}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:self];
}

-(BOOL)validateUserName{
    
    NSString *userNameRegex = @"^[_]{0,1}[0-9a-zA-Z]{6,25}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    return [userNamePredicate evaluateWithObject:self];
}
//身份证号码
-(BOOL) validateIdentityCard{
    
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *validateIdentityCard = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [validateIdentityCard evaluateWithObject:self];
}


//昵称
-(BOOL) validateNickname
{
//    NSString *nicknameRegex = @"^[_]{0,1}[0-9a-zA-Z]{6,25}+$";
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5A-Za-z0-9-_]*$";
    NSPredicate *validateNickname = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [validateNickname evaluateWithObject:self];
}

- (BOOL)isChinese {
    NSString *match = @"(^[u4e00-u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}

- (BOOL)includeChinese {
    for(int i=0; i< [self length];i++) {
        int a =[self characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}

//姓名
-(BOOL)name
{
    NSString *Myname = @"^[_]{0,1}[0-9a-zA-Z]{6,30}+$";
    NSPredicate *name = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Myname];
    return [name evaluateWithObject:self];
}

-(BOOL)validateLetterAndNumberOnly {
    NSString *lm = @"^[A-Za-z0-9]+$";
    NSPredicate *name = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",lm];
    return [name evaluateWithObject:self];
}

-(BOOL)validateLetterOnly {
    NSString *lm = @"^[A-Za-z]+$";
    NSPredicate *name = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",lm];
    return [name evaluateWithObject:self];
}

-(BOOL)validateNumberOnly {
    NSString *lm = @"^[0-9]+$";
    NSPredicate *name = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",lm];
    return [name evaluateWithObject:self];
}

-(BOOL)validateSpecailCharacters {
    NSString * nameCharacters =@"^[A-Za-z0-9\u4E00-\u9FA5_-]+$";
    NSPredicate * isSpecialCharacter = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",nameCharacters];
    return [isSpecialCharacter evaluateWithObject:self];
}

@end
